

### This script contains the code necessary to replicate the analyses for the article "An Emotional Climate: Legislators' Emotional Engagement with Climate Issues across Age, Party and Time" 
### by Julius Diener published in European Journal of Political Research (2026). 
### If you have any additional questions feel free to contact me under my current contact information listed on my website https://juliusdiener.com/


## Load packages

##
p_needed <-
  c("here",
    "lme4",
    "viridis",
    "plm",
    "texreg",
    "lubridate",
    "dplyr",
    "stringr",
    "here",
    "MASS",
    "parameters",
    "readxl",
    "caret",
    "xtable",
    "scales",
    "diagram",
    "quanteda",
    "tidyverse",
    "ggplot2",
    "cowplot",
    "lubridate")

packages <- rownames(installed.packages())

p_to_install <- p_needed[!(p_needed %in% packages)]

if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}

sapply(p_needed, require, character.only = TRUE)



## Load Data

load("data_pitch_climate.RData")


## Some new variables 
data_pitch_climate$year <- year(data_pitch_climate$session_date)

data_pitch_climate <- data_pitch_climate %>% filter(party != "Plos")

data_pitch_climate$SPD <- ifelse(data_pitch_climate$party == "SPD", 1, 0)
data_pitch_climate$CDU <- ifelse(data_pitch_climate$party == "CDU", 1, 0)
data_pitch_climate$Grüne <- ifelse(data_pitch_climate$party == "BÜNDNIS 90/DIE GRÜNEN", 1, 0)
data_pitch_climate$FDP <- ifelse(data_pitch_climate$party == "FDP", 1, 0)
data_pitch_climate$Linke <- ifelse(data_pitch_climate$party == "DIE LINKE.", 1, 0)
data_pitch_climate$CSU <- ifelse(data_pitch_climate$party == "CSU", 1, 0)
data_pitch_climate$AfD <- ifelse(data_pitch_climate$party == "AfD", 1, 0)

data_pitch_climate$opposition <- ifelse(data_pitch_climate$position == "Opposition", 1, 0)



## Descriptives

summary(data_pitch_climate)

table(data_pitch_climate$mentions_environment_roberta)

ggplot(data = data_pitch_climate[data_pitch_climate$mentions_environment_roberta == 1,]) +
  geom_histogram(aes(x = session_date), bins = 45) +
  theme_bw(base_size = 22) +
  labs(x = "Date",
       y = "# Speeches Mentioning Climate")

length(unique(data_pitch_climate$speaker_id))

freq_speakers <- data_pitch_climate %>% dplyr::group_by(speaker_id) %>% summarize(total = n(), total_climate = sum(mentions_environment_roberta))
freq_speakers$total_not_climate <- freq_speakers$total - freq_speakers$total_climate

desc_total <- ggplot(data = freq_speakers) +
  geom_histogram(aes(x = total), fill = "grey") +
  geom_vline(xintercept = mean(freq_speakers$total), linetype = "dashed", color = "red") +
  theme_bw() +
  labs(x = "Total Speeches per MP",
       y = "Count",
       caption = paste0("Dashed line = mean (", round(mean(freq_speakers$total), digits = 1), " speeches)"))

desc_climate <- ggplot(data = freq_speakers) +
  geom_histogram(aes(x = total_climate), fill = "grey") +
  geom_vline(xintercept = mean(freq_speakers$total_climate), linetype = "dashed", color = "red") +
  theme_bw() +
  labs(x = "Total Speeches mentioning Climate Issues per MP",
       y = "Count",
       caption = paste0("Dashed line = mean (", round(mean(freq_speakers$total_climate), digits = 1), " speeches mentioning climate issues)"))

plot_grid(desc_total, desc_climate)





## models

# exclude non MPs
data_pitch_climate <- data_pitch_climate[which(data_pitch_climate$mdb),]


# DV = Pitch continuous

m_env_base <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                   AfD + CSU + FDP + Grüne + Linke + SPD + year +
                   as.factor(speaker_id),
                 data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                  mentions_environment_roberta * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                  as.factor(speaker_id),
                data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                    mentions_environment_roberta * AfD +
                    mentions_environment_roberta * CSU +
                    mentions_environment_roberta * FDP +
                    mentions_environment_roberta * Grüne +
                    mentions_environment_roberta * Linke +
                    mentions_environment_roberta * SPD + 
                    + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                    as.factor(speaker_id),
                  data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                   mentions_environment_roberta * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                   as.factor(speaker_id),
                 data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

models <- list(m_env_base, m_env_age, m_env_party, m_env_year)

texreg::texreg(models,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               label = "table:pitch_results",
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

texreg::wordreg(models,
                custom.coef.names = c("Intercept",
                                      "Climate mentioned",
                                      "Age",
                                      "AfD",
                                      "CSU",
                                      "FDP",
                                      "Grüne",
                                      "Linke",
                                      "SPD",
                                      "Year",
                                      "Climate mentioned $\\times$ Age",
                                      "Climate mentioned $\\times$ AfD",
                                      "Climate mentioned $\\times$ CSU",
                                      "Climate mentioned $\\times$ FDP",
                                      "Climate mentioned $\\times$ Grüne",
                                      "Climate mentioned $\\times$ Linke",
                                      "Climate mentioned $\\times$ SPD",
                                      "Climate mentioned $\\times$ Year"
                ),
                omit.coef = "speaker_name|speaker_id",
                custom.model.names = c("Base", "Age", "Parties", "Year"),
                caption = "Effects on Emotional Intensity when speaking on Climate Issues",
                custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
                caption.above = F,
                label = "table:pitch_results",
                booktabs = T,
                digits = 3,
                stars = 0.05,
                float.pos = "t",
                scalebox = 0.85,
                file = "main_table_rr1.doc")



#ROB: Alternative standardization


# speaker averages
legislator_pitch_mean <- aggregate(pitch_comb ~ speaker_id,
                                   data = data_pitch_climate,
                                   FUN = mean)
# into data
data_pitch_climate$baseline_pitch <- 
  legislator_pitch_mean$pitch_comb[match(data_pitch_climate$speaker_id,
                                         legislator_pitch_mean$speaker_id)]




# de-meaned pitch 
data_pitch_climate <- cbind(data_pitch_climate,
               parameters::demean(data_pitch_climate,
                                  select = c("pitch_comb"),
                                  by = c("speaker_id")))


# standardized sd but not de-meaned
data_pitch_climate$pitch_stand_sd <- data_pitch_climate$pitch_z_stand + data_pitch_climate$pitch_comb_between
# alternative way to get the same thing
data_pitch_climate$pitch_stand_sd2 <- data_pitch_climate$pitch_z_stand + data_pitch_climate$baseline_pitch

table(data_pitch_climate$pitch_stand_sd == data_pitch_climate$pitch_stand_sd2)


m_env_base_alt <- lm(data_pitch_climate$pitch_stand_sd ~ mentions_environment_roberta + age +
                       AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age_alt <- lm(data_pitch_climate$pitch_stand_sd ~ mentions_environment_roberta + age +
                      mentions_environment_roberta * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party_alt <- lm(data_pitch_climate$pitch_stand_sd ~ mentions_environment_roberta + age +
                        mentions_environment_roberta * AfD +
                        mentions_environment_roberta * CSU +
                        mentions_environment_roberta * FDP +
                        mentions_environment_roberta * Grüne +
                        mentions_environment_roberta * Linke +
                        mentions_environment_roberta * SPD + 
                        + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                        as.factor(speaker_id),
                      data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year_alt <- lm(data_pitch_climate$pitch_stand_sd ~ mentions_environment_roberta + age +
                       mentions_environment_roberta * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])


models_alt <- list(m_env_base_alt, m_env_age_alt, m_env_party_alt, m_env_year_alt)

texreg::texreg(models_alt,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Party", "Year"),
               caption = "Robustness: Effects on emotional intensity when speaking on climate issues using alternative standartization",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               label = "table:pitch_results",
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

# ROB: other thresholds

data_pitch_climate$sent_threshold_5 <- data_pitch_climate$n_sent / 20
data_pitch_climate$sent_threshold_15 <- data_pitch_climate$n_sent / 100 * 15
data_pitch_climate$sent_threshold_20 <- data_pitch_climate$n_sent / 5

data_pitch_climate$mentions_environment_roberta_one <- ifelse(data_pitch_climate$climate.x >= 1, 1, 0)
data_pitch_climate$mentions_environment_roberta_5 <- ifelse(data_pitch_climate$climate.x >= data_pitch_climate$sent_threshold_5, 1, 0)
data_pitch_climate$mentions_environment_roberta_15 <- ifelse(data_pitch_climate$climate.x >= data_pitch_climate$sent_threshold_15, 1, 0)
data_pitch_climate$mentions_environment_roberta_20 <- ifelse(data_pitch_climate$climate.x >= data_pitch_climate$sent_threshold_20, 1, 0)

# 1 Sentence

m_env_base_one <- lm(pitch_z_stand ~ mentions_environment_roberta_one + age +
                       AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age_one <- lm(pitch_z_stand ~ mentions_environment_roberta_one + age +
                      mentions_environment_roberta_one * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party_one <- lm(pitch_z_stand ~ mentions_environment_roberta_one + age +
                        mentions_environment_roberta_one * AfD +
                        mentions_environment_roberta_one * CSU +
                        mentions_environment_roberta_one * FDP +
                        mentions_environment_roberta_one * Grüne +
                        mentions_environment_roberta_one * Linke +
                        mentions_environment_roberta_one * SPD + 
                        + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                        as.factor(speaker_id),
                      data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year_one <- lm(pitch_z_stand ~ mentions_environment_roberta_one + age +
                       mentions_environment_roberta_one * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

models_one <- list(m_env_base_one, m_env_age_one, m_env_party_one, m_env_year_one)

texreg::texreg(models_one,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues using one Sentence Mentioning Climate Issues as alternative Cut-Off",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

# 5 Percent

m_env_base_5 <- lm(pitch_z_stand ~ mentions_environment_roberta_5 + age +
                     AfD + CSU + FDP + Grüne + Linke + SPD + year +
                     as.factor(speaker_id),
                   data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age_5 <- lm(pitch_z_stand ~ mentions_environment_roberta_5 + age +
                    mentions_environment_roberta_5 * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                    as.factor(speaker_id),
                  data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party_5 <- lm(pitch_z_stand ~ mentions_environment_roberta_5 + age +
                      mentions_environment_roberta_5 * AfD +
                      mentions_environment_roberta_5 * CSU +
                      mentions_environment_roberta_5 * FDP +
                      mentions_environment_roberta_5 * Grüne +
                      mentions_environment_roberta_5 * Linke +
                      mentions_environment_roberta_5 * SPD + 
                      + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year_5 <- lm(pitch_z_stand ~ mentions_environment_roberta_5 + age +
                     mentions_environment_roberta_5 * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                     as.factor(speaker_id),
                   data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

models_5 <- list(m_env_base_5, m_env_age_5, m_env_party_5, m_env_year_5)

texreg::texreg(models_5,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues using 5 Percent of Sentences Mentioning Climate Issues as alternative Cut-Off",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

# 15 Percent

m_env_base_15 <- lm(pitch_z_stand ~ mentions_environment_roberta_15 + age +
                      AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age_15 <- lm(pitch_z_stand ~ mentions_environment_roberta_15 + age +
                     mentions_environment_roberta_15 * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                     as.factor(speaker_id),
                   data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party_15 <- lm(pitch_z_stand ~ mentions_environment_roberta_15 + age +
                       mentions_environment_roberta_15 * AfD +
                       mentions_environment_roberta_15 * CSU +
                       mentions_environment_roberta_15 * FDP +
                       mentions_environment_roberta_15 * Grüne +
                       mentions_environment_roberta_15 * Linke +
                       mentions_environment_roberta_15 * SPD + 
                       + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year_15 <- lm(pitch_z_stand ~ mentions_environment_roberta_15 + age +
                      mentions_environment_roberta_15 * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

models_15 <- list(m_env_base_15, m_env_age_15, m_env_party_15, m_env_year_15)

texreg::texreg(models_15,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues using 15 Percent of Sentences Mentioning Climate Issues as alternative Cut-Off",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)




# ROB: Continuous climate measure

data_pitch_climate$mentions_environment_con <- data_pitch_climate$climate.x / data_pitch_climate$n_sent


m_env_base_con <- lm(pitch_z_stand ~ mentions_environment_con + age +
                       AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age_con <- lm(pitch_z_stand ~ mentions_environment_con + age +
                      mentions_environment_con * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party_con <- lm(pitch_z_stand ~ mentions_environment_con + age +
                        mentions_environment_con * AfD +
                        mentions_environment_con * CSU +
                        mentions_environment_con * FDP +
                        mentions_environment_con * Grüne +
                        mentions_environment_con * Linke +
                        mentions_environment_con * SPD + 
                        + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                        as.factor(speaker_id),
                      data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year_con <- lm(pitch_z_stand ~ mentions_environment_con + age +
                       mentions_environment_con * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

models_con <- list(m_env_base_con, m_env_age_con, m_env_party_con, m_env_year_con)

texreg::texreg(models_con,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned Continuous",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned Continuous $\\times$ Age",
                                     "``Climate'' mentioned Continuous $\\times$ AfD",
                                     "``Climate'' mentioned Continuous $\\times$ CSU",
                                     "``Climate'' mentioned Continuous $\\times$ FDP",
                                     "``Climate'' mentioned Continuous $\\times$ Grüne",
                                     "``Climate'' mentioned Continuous $\\times$ Linke",
                                     "``Climate'' mentioned Continuous $\\times$ SPD",
                                     "``Climate'' mentioned Continuous $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues with continuous measure of climate issue mentions",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               label = "table:pitch_results",
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

# ROB: Extended Models

m_env_base_ext <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                       AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       interruptions_per_day + years_in_parliament + party_leadership + position +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_age_ext <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                      mentions_environment_roberta * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                      interruptions_per_day + years_in_parliament + party_leadership + position +
                      as.factor(speaker_id),
                    data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_party_ext <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                        mentions_environment_roberta * AfD +
                        mentions_environment_roberta * CSU +
                        mentions_environment_roberta * FDP +
                        mentions_environment_roberta * Grüne +
                        mentions_environment_roberta * Linke +
                        mentions_environment_roberta * SPD + 
                        + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                        interruptions_per_day + years_in_parliament + party_leadership + position +
                        as.factor(speaker_id),
                      data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

m_env_year_ext <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                       mentions_environment_roberta * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       interruptions_per_day + years_in_parliament + party_leadership + position +
                       as.factor(speaker_id),
                     data = data_pitch_climate[data_pitch_climate$speech_n_words > 50 & data_pitch_climate$mdb,])

models_ext <- list(m_env_base_ext, m_env_age_ext, m_env_party_ext, m_env_year_ext)

texreg::texreg(models_ext,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "Interruptions per Day",
                                     "Years in Parliament",
                                     "Party Leadership",
                                     "Opposition",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues with additional controls",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               label = "table:pitch_results",
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

# ROB: Only speakers with 3 speeches on climate and not on climate



freq_speakers_2 <- freq_speakers %>% filter(total_climate >= 3 & total_not_climate >= 3)

freq_subset <- data_pitch_climate %>% filter(speaker_id %in% freq_speakers_2$speaker_id)

m_env_base_freq <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                        AfD + CSU + FDP + Grüne + Linke + SPD + year +
                        as.factor(speaker_id),
                      data = freq_subset[freq_subset$speech_n_words > 50 & freq_subset$mdb,])

m_env_age_freq <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                       mentions_environment_roberta * age + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                       as.factor(speaker_id),
                     data = freq_subset[freq_subset$speech_n_words > 50 & freq_subset$mdb,])

m_env_party_freq <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                         mentions_environment_roberta * AfD +
                         mentions_environment_roberta * CSU +
                         mentions_environment_roberta * FDP +
                         mentions_environment_roberta * Grüne +
                         mentions_environment_roberta * Linke +
                         mentions_environment_roberta * SPD + 
                         + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                         as.factor(speaker_id),
                       data = freq_subset[freq_subset$speech_n_words > 50 & freq_subset$mdb,])

m_env_year_freq <- lm(pitch_z_stand ~ mentions_environment_roberta + age +
                        mentions_environment_roberta * year + AfD + CSU + FDP + Grüne + Linke + SPD + year +
                        as.factor(speaker_id),
                      data = freq_subset[freq_subset$speech_n_words > 50 & freq_subset$mdb,])

models_freq <- list(m_env_base_freq, m_env_age_freq, m_env_party_freq, m_env_year_freq)

texreg::texreg(models_freq,
               custom.coef.names = c("Intercept",
                                     "``Climate'' mentioned",
                                     "Age",
                                     "AfD",
                                     "CSU",
                                     "FDP",
                                     "Grüne",
                                     "Linke",
                                     "SPD",
                                     "Year",
                                     "``Climate'' mentioned $\\times$ Age",
                                     "``Climate'' mentioned $\\times$ AfD",
                                     "``Climate'' mentioned $\\times$ CSU",
                                     "``Climate'' mentioned $\\times$ FDP",
                                     "``Climate'' mentioned $\\times$ Grüne",
                                     "``Climate'' mentioned $\\times$ Linke",
                                     "``Climate'' mentioned $\\times$ SPD",
                                     "``Climate'' mentioned $\\times$ Year"
               ),
               omit.coef = "speaker_name|speaker_id",
               custom.model.names = c("Base", "Age", "Parties", "Year"),
               caption = "Effects on Emotional Intensity when speaking on Climate Issues",
               custom.note = "Fixed-Effects Models with varying intercepts on the legislator level. \\
               * = p<0.05",
               caption.above = F,
               label = "table:pitch_results",
               booktabs = T,
               digits = 3,
               stars = 0.05,
               float.pos = "t",
               scalebox = 0.85)

# marginal effects

mfx_lm <- function (b, z) {
  b["mentions_environment_roberta"] + b["mentions_environment_roberta:year"] * z
}

se_mfx <- function (vcov, z) {
  a <- "mentions_environment_roberta"
  b <- "mentions_environment_roberta:year"
  sqrt(vcov[a, a] + z ^ 2 * vcov[b, b] + 2 * z * vcov[b, a])
}

year_range <- seq(round(min(data_pitch_climate$year)), round(max(data_pitch_climate$year)), 1)

mar_eff <- data.frame(year = rep(NA, round(max(data_pitch_climate$year))),
                      mar_eff = rep(NA, round(max(data_pitch_climate$year))),
                      se = rep(NA, round(max(data_pitch_climate$year))))



for (i in year_range) {
  mar_eff[i,1] <- i
  mar_eff[i,2] <- mfx_lm(b = coef(m_env_year), z = i)
  mar_eff[i,3] <- se_mfx(vcov = vcov(m_env_year), z = i)
}

mar_eff <- mar_eff[min(data_pitch_climate$year):nrow(mar_eff),]

mar_eff$lower <- NA
mar_eff$upper <- NA

for (i in 1:nrow(mar_eff)) {
  mar_eff[i,4] <- qnorm(0.025, mar_eff[i,2], mar_eff[i,3])
  mar_eff[i,5] <- qnorm(0.975, mar_eff[i,2], mar_eff[i,3])
}


## plot marginal effect

library(ggplot2)

ggplot(data = mar_eff) +
  geom_line(aes(x = year, y = mar_eff)) +
  geom_ribbon(aes(x = year, ymin = lower, ymax = upper), color = "lightgray", alpha = 0.3) +
  theme_bw(base_size = 25) +
  scale_x_continuous(breaks = c(2012, 2014, 2016, 2018, 2020)) +
  labs(x = "Year",
       y = "Marginal Effect",
       caption = "Shaded Area = 95% CIs")










